﻿package {
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.text.*;
	
	/** Called by an <cimg> tag in the xml.  Uses a sequence of <img> tags directing to image files in directory.
	*  by default builds all images from left to right. */
	public class TextImageLine extends Sprite {
		private static const X_BORDER_PADDING:int = 10;
		public static const X_PADDING:int = 10;
		public var item_number:int;
		public var title:String="";
		public var showNum:Boolean = false;
		public var numberTextField:TextField;
		private var img:XML, text:XML, cimg:XML;
		private var tfield:TextField;
		private var ldr:Loader;
		private var totalImages:int, currentImage:int;
		private var currentX:Number, currentY:Number;
		//ldrArray:Array;
		
		public function TextImageLine (){
		}
		public function load(cimg:XML, item_number:int):void
		{
			this.cimg = cimg;
			this.item_number = item_number;
			title = "";
			currentX = X_BORDER_PADDING; currentY = 0;
			totalImages = cimg.child("*").length();
			currentImage = 0;
			addNew();
		}
		public function redraw():void
		{
			for (var i:int = this.numChildren-1; i < 0; i++){
				this.removeChildAt(i);
			}
			currentImage = 0;
			addNew();
		}
		private function addNew():void
		{
			var o:XML = cimg.child("*")[currentImage];
			if (title == ""){
				title = String(o);
			} else {
				title += "-"+String(o); 
			}
			if(o.name()=="img"){
				addNewImage(o);
			} else if (o.name()=="text"){
				addNewText(o);
			} 
		}
		/** Add a new image for a file within the app's image directory */
		private function addNewImage(o:XML):void
		{
			//img = cimg.child("*")[currentImage];
			img = o;
			ldr = new Loader();
			//trace(String("images/"+img));
			var urlReq:URLRequest = new URLRequest(String("images/"+img));
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, handleLoaded);
			try {
				ldr.load(urlReq);
			} catch (e:IOErrorEvent){
				ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleLoaded);				
				trace(String(img), " does not exist.");
				this.dispatchEvent(new Event(IOErrorEvent.IO_ERROR));
			}
		}
		
		private function handleLoaded(event:Event):void
		{
			ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleLoaded);
			ldr.mouseEnabled = false;
			ldr.x = currentX;
			ldr.y = currentY;
			currentX += ldr.width + X_PADDING;
			//if (this.width < currentX) 	this.width = currentX;
			this.addChild(ldr);
			
			currentImage++;
			if (currentImage < totalImages){
				addNew();
			} else {
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		/** Create a standard Textfield */
		private function addNewText(t:XML):void
		{
			text = t;
			tfield = new TextField();
			tfield.selectable = false;
			tfield.mouseEnabled = false;
			tfield.defaultTextFormat = new TextFormat("Arial", 16, 0x000000,null,null,null,null,null,TextFormatAlign.LEFT);
			tfield.text = String(text).split("[n]").join("\n ");;
			tfield.text = tfield.text.split("[t]").join("     ");
				if (text.attribute("showNum").length() > 0 && text.attribute("showNum")[0] == "true"){
				tfield.text = String(item_number + ".) " + tfield.text);
			} 
			tfield.autoSize = "left";
			tfield.x = currentX;
			tfield.y = currentY;
			currentX += tfield.width + X_PADDING;
			this.addChild(tfield);
			currentImage++;
			if (currentImage < totalImages){
				addNew();
			} else {
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
	}
}